package com.wlts.wzy.KSGoodsModule.controller;


import com.kuaishou.merchant.open.api.KsMerchantApiException;
import com.kuaishou.merchant.open.api.client.AccessTokenKsMerchantClient;
import com.kuaishou.merchant.open.api.request.KsMerchantExpressTemplateListRequest;
import com.kuaishou.merchant.open.api.response.KsMerchantExpressTemplateListResponse;
import com.wlts.wzy.Base.BasePageParam;
import com.wlts.wzy.KSUserModule.entity.KsOauth;
import com.wlts.wzy.KSUserModule.entity.KsStoreUser;
import com.wlts.wzy.KSUserModule.util.KsConfig;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.configBean.KsConfig.KsConfigInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 快手商品类目表 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-01-31
 */
@Api(tags = "快手运费模板 前端控制器")
@RestController
@RequestMapping("/KSGoodsModule/KsExpressTemplate")
public class KsExpressTemplateListController {
    @Autowired
    private KsConfig ksConfig;

    @ApiModelProperty("分页快手运费模板")
    @RequestMapping(value = "getKsTemplate", method = RequestMethod.POST)
    public Result<KsMerchantExpressTemplateListResponse> getKsTemplate(@RequestBody BasePageParam page) throws KsMerchantApiException {

        KsStoreUser ksStoreUser = ksConfig.getKsStoreUserConfig();

        KsOauth ksOauth = ksStoreUser.getKsOauth();

        if (ksOauth == null) {
            return Result.buildResult(500,"账号未授权");
        }


        KsMerchantExpressTemplateListRequest request = new KsMerchantExpressTemplateListRequest();
        request.setAccessToken(ksOauth.getAccessToken());
        request.setLimit(page.getPageSize());
        request.setOffset((page.getCurrentPage() - 1) * page.getPageSize());
        request.setSearchUsed(true);

//        System.out.println(KsConfigInfo.appKey);
//        System.out.println(KsConfigInfo.signSecret);
//
//        System.out.println(KsConfigInfo.signSecret.equals("554a351fd517a39d6860d91d29b92a13"));
        AccessTokenKsMerchantClient tokenKsMerchantClient
                = new AccessTokenKsMerchantClient("", KsConfigInfo.appKey, KsConfigInfo.signSecret);
        KsMerchantExpressTemplateListResponse response = tokenKsMerchantClient.execute(request);
        return Result.buildResult(response);
    }

}